package com.wolf.etl.core;

import com.wolf.etl.enumeration.DB_TYPE;
import com.wolf.etl.vo.ColumnDesc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tommy's father on 2017/8/30.
 */
@Service("ColumnDescManager")
public class ColumnDescManager {

    private static Logger logger = LoggerFactory.getLogger(ColumnDescManager.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.core#ColumnDescManager";


    public static ColumnDesc getConvertDesc(int dbType) {

        // 防止引用传参，新建一个对象重新赋值 added by sdyang 2019-09-22
        ColumnDesc desc = new ColumnDesc();
        desc.setJdbcType(dbType);

        String strType = DB_TYPE.valueOf(dbType).toString();

        //logger.info(String.format("数据类型：%s",strType));

        for (ColumnDesc columnDesc : infos) {
            for (String type : columnDesc.getDbTypes()) {
                if (type.equalsIgnoreCase(strType)) {
                    //logger.info(String.format("%s  ----------%s ",strType,type));
                    BeanUtils.copyProperties(columnDesc,desc);
                }
            }
        }
        return desc;
    }

    private static String[][] Type2Methods = new String[][]{{"java.lang.String", "getString"},
            {"boolean", "getBoolean"},
            {"byte", "getByte"},
            {"short", "getShort"},
            {"int", "getInt"},
            {"long", "getLong"},
            {"float", "getFloat"},
            {"double", "getDouble"},
            {"BigDecimal", "getBigDecimal"},
            {"byte[]", "getBytes"},
            {"java.sql.Date", "getDate"},
            {"java.sql.Time", "getTime"},
            {"java.sql.Timestamp", "getTimestamp"},
            {"Ref", "getRef"},
            {"Blob", "getBlob"},
            {"Clob", "getClob"},
            {"Array", "getArray"},
            {"RowId", "getRowId"},
            {"NClob", "getNClob"},
            {"SQLXML", "getSQLXML"},
            {"NString", "getNString"}};

    private static String[][] Type2DBType = new String[][]{{"java.lang.String", "VARCHAR#CHAR#NString"},
            {"boolean", "TINYINT"},
            {"byte", "BIT"},
            {"short", "SMALLINT"},
            {"int", "INTEGER#MEDIUMINT"},
            {"long", "BIGINT"},
            {"float", "FLOAT"},
            {"double", "DOUBLE"},
            {"BigDecimal", "REAL#DECIMAL#NUMERIC"},
            {"byte[]", "BINARY#VARBINARY"},
            {"java.sql.Date", "DATE"},
            {"java.sql.Time", "TIME"},
            {"java.sql.Timestamp", "TIMESTAMP"},
            {"Ref", "REF"},
            {"Blob", "BLOB"},
            {"Clob", "CLOB"},
            {"Array", "ARRAY"},
            {"RowId", "ROWID"},
            {"NClob", "NCLOB"},
            {"SQLXML", "SQLXML"},
            {"NString", "NCHAR#NVARCHAR#LONGNVARCHAR"}};

    static List<ColumnDesc> infos = new ArrayList<ColumnDesc>();

    static {
        for (int i = 0; i < Type2Methods.length; i++) {
            ColumnDesc info = new ColumnDesc();
            info.setClassName(Type2Methods[i][0]);
            info.setMethodName(Type2Methods[i][1]);
            for (int j = 0; j < Type2DBType.length; j++) {
                if (Type2Methods[i][0] == Type2DBType[j][0]) {
                    List<String> dbTypes = new ArrayList<String>();
                    String strSplit = Type2DBType[j][1];
                    if (strSplit.indexOf("#") == -1) {
                        dbTypes.add(strSplit);
                        info.setDbTypes(dbTypes);
                        continue;
                    }

                    String[] types = strSplit.split("#");
                    for (int p = 0; p < types.length; p++) {
                        dbTypes.add(types[p]);
                    }
                    info.setDbTypes(dbTypes);
                }
            }
            infos.add(info);
        }
    }
}
